package com.swak.frame.dto.response;

import java.util.Map;

import com.google.common.collect.Maps;
import com.swak.frame.dto.base.RestApiCode;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * Response with result {@link Response}
 * 
 * @ClassName: RpcResponse.java
 * @author: colley.ma
 * @date: 2022/01/25
 */
@ApiModel(value = "RpcResponse结果")
public class RpcResponse<T> extends Response {
	private static final long serialVersionUID = 3375048578170714304L;

	@ApiModelProperty(value = "响应结果")
	protected T result;

	@ApiModelProperty(value = "扩展信息，保留字段")
	private Map<String, Object> extension;

	public RpcResponse(String retCode, String retMessage) {
		super(retCode, retMessage);
	}

	public RpcResponse(RestApiCode restCode) {
		this(restCode.getRetCode(), restCode.getRetMessage());
	}

	public RpcResponse() {
		super();
	}

	public RpcResponse(T result) {
		super();
		this.result = result;
	}

	protected RpcResponse(Builder<T> builder) {
		super(builder);
		this.result = builder.result;
		this.extension = builder.extension;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	public Map<String, Object> getExtension() {
		return extension;
	}

	public void setExtension(Map<String, Object> extension) {
		this.extension = extension;
	}

	public RpcResponse<T> ofResult(T data) {
		this.result = data;
		return this;
	}

	@Override
	public RpcResponse<T> of(RestApiCode restCode) {
		super.of(restCode);
		return this;
	}

	@Override
	public RpcResponse<T> of(String retCode, String retMessage) {
		super.of(retCode, retMessage);
		return this;
	}

	public static class Builder<T> extends Response.Builder {
		protected T result;
		protected Map<String, Object> extension;

		public Builder() {
			super();
		}

		@Override
		public Builder<T> retCode(String retCode) {
			super.retCode(retCode);
			return this;
		}

		@Override
		public Builder<T> of(RestApiCode restCode) {
			super.of(restCode);
			return this;
		}

		@Override
		public Builder<T> retMessage(String retMessage) {
			super.retMessage(retMessage);
			return this;
		}

		@Override
		public Builder<T> of(String retCode, String retMessage) {
			super.of(retCode, retMessage);
			return this;
		}

		public Builder<T> result(T result) {
			this.result = result;
			return this;
		}

		public Builder<T> extension(Map<String, Object> extension) {
			this.extension = extension;
			return this;
		}

		public Builder<T> extension(String key, Object value) {
			if (this.extension == null) {
				this.extension = Maps.newHashMap();
			}
			this.extension.put(key, value);
			return this;
		}

		@Override
		public RpcResponse<T> build() {
			return new RpcResponse<T>(this);
		}
	}
}
